//
//  SleepActivityStatusCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/4.
//

import UIKit
import MYModule

import RxSwift
import RxCocoa

open class SleepActivityStatusCell: SleepBaseCell {
    
    var model: SleepActivityStatusModel? {
        
        didSet {
            
            chartViewReloadData(model: model)
        }
        
    }
    
    let line = DottedLine()
    
    let titleLabel = Label()
    
    let fallAsleepLabel = Label()
    
    let wakeUpLabel = Label()
    
    let littleActivityView = ActivityView()
    
    let moderateActivityView = ActivityView()
    
    let frequentActivityView = ActivityView()
    
    lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 2, height: 51)
        layout.sectionInset = UIEdgeInsets.zero
        layout.minimumLineSpacing = 1.0
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .horizontal
        
        return layout
    }()
    
    lazy var collectionView: CollectionView = {
        
        let collectionView = CollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        
        return collectionView
        
    }()
    
    open override func makeUI() {
        super.makeUI()
        
        titleLabel.text = "sleep_activity_status".language
        titleLabel.font = Fonts.boldSystemFont(ofSize: 14)
        titleLabel.textColor = UIColor.assist_222222
        
        fallAsleepLabel.textColor = UIColor.assist_A9A9A9
        fallAsleepLabel.font = Fonts.systemFont(ofSize: 12)
        
        wakeUpLabel.textColor = UIColor.assist_A9A9A9
        wakeUpLabel.font = Fonts.systemFont(ofSize: 12)
        
        littleActivityView.icon.backgroundColor = UIColor.Sleep.awake
        littleActivityView.titleLabel.text = "sleep_little_activity".language
        
        moderateActivityView.icon.backgroundColor = UIColor.Sleep.rem
        moderateActivityView.titleLabel.text = "sleep_moderate_activity".language
        
        frequentActivityView.icon.backgroundColor = UIColor.Sleep.light
        frequentActivityView.titleLabel.text = "sleep_frequent_activity".language
        
        
        collectionView.register(SleepActivityStatusBarCell.self, forCellWithReuseIdentifier: SleepActivityStatusBarCell.reuseIdentifier)
        collectionView.dataSource = self
        
        
        bgView.addSubview(titleLabel)
        bgView.addSubview(collectionView)
        bgView.addSubview(fallAsleepLabel)
        bgView.addSubview(wakeUpLabel)
        bgView.addSubview(littleActivityView)
        bgView.addSubview(moderateActivityView)
        bgView.addSubview(frequentActivityView)
        bgView.addSubview(line)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(14)
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-20)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(51)
        }
        
        fallAsleepLabel.snp.makeConstraints { make in
            make.left.equalTo(collectionView.snp.left)
            make.top.equalTo(collectionView.snp.bottom).offset(10)
        }
        
        wakeUpLabel.snp.makeConstraints { make in
            make.right.equalTo(collectionView.snp.right)
            make.top.equalTo(fallAsleepLabel.snp.top)
        }
        
        littleActivityView.snp.makeConstraints { make in
            make.left.equalTo(22.5)
            make.top.equalTo(wakeUpLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(-20)
        }
        
        moderateActivityView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(littleActivityView.snp.top)
        }
        
        frequentActivityView.snp.makeConstraints { make in
            make.right.equalTo(-22.5)
            make.top.equalTo(littleActivityView.snp.top)
        }
        
        line.snp.makeConstraints { make in
            make.centerY.equalTo(collectionView.snp.centerY)
            make.left.equalTo(collectionView.snp.left)
            make.right.equalTo(collectionView.snp.right)
            make.height.equalTo(1.0)
        }
    }
    
    
    public override func bind(to cellType: SleepViewModel.CellType) {
        super.bind(to: cellType)
        
        guard let viewModel = cellType.viewModel as? SleepActivityStatusViewModel else { return }
        
        let disposeBag = DisposeBag()
        
        viewModel.UIRePlay.subscribe(onNext: {[weak self] in
            
            self?.fallAsleepLabel.text = $0?.fallAsleepTitle
            self?.wakeUpLabel.text = $0?.wakeUpTitle
            
            self?.model = $0
            
            
        }).disposed(by: disposeBag)
        
        
    }
    
    func chartViewReloadData(model: SleepActivityStatusModel?) {
        
        guard let model = model, let chartDatas = model.sleepingChartData else {
            return
        }

        
        if chartDatas.count > 0 {
            let width = (UIScreen.main.bounds.size.width - 40)/CGFloat(chartDatas.count)
            layout.itemSize = CGSize(width: width, height: layout.itemSize.height)
        }
        
        collectionView.collectionViewLayout = layout
        
        collectionView.reloadData()
    }
    
}

extension SleepActivityStatusCell: UICollectionViewDataSource {
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model?.sleepingChartData?.count ?? 0
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SleepActivityStatusBarCell.reuseIdentifier, for: indexPath) as! SleepActivityStatusBarCell
        
        cell.sleepData = model?.sleepingChartData?[indexPath.item]
        
        return cell
        
    }
    
    
    
}

// MARK: - collectionCell
extension SleepActivityStatusCell {
    
    open class SleepActivityStatusBarCell: CollectionViewCell {
        
        
        var sleepData: SleepActivityStatusChartData? {
            
            didSet {
                
                bar.backgroundColor = sleepData?.color
                
                guard let sleepData = sleepData else {
                    return
                }

                bar.snp.updateConstraints { make in
                    make.height.equalTo(sleepData.viewHeight)
                }
                
            }
            
        }
        
        let bar = View()
        
        open override func makeUI() {
            super.makeUI()
            
            contentView.addSubview(bar)
            
            
            bar.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.centerX.equalToSuperview()
                make.width.equalTo(2)
                make.height.equalTo(0)
            }
            
        }
        
    }
    
    
}

//MARK: - DottedLine
extension SleepActivityStatusCell {
    
    class DottedLine: View {
        
        let lineColor = UIColor.Sleep.theme
        
        let lineLayer = CAShapeLayer()
        
        
        override func makeUI() {
            super.makeUI()
            
            lineLayer.strokeColor = lineColor.cgColor
            lineLayer.frame = self.bounds
            lineLayer.lineWidth = 1.0
            lineLayer.lineDashPattern = [1.5, 1.5]
            
            layer.addSublayer(lineLayer)
            
        }
        
        var path: UIBezierPath {
            
            let path = UIBezierPath()
            path.move(to: CGPoint(x: 0, y: self.bounds.midY))
            path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.midY))
            return path
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            
            lineLayer.frame = self.bounds
            lineLayer.path = path.cgPath
            
        }
        
    }
}

// MARK: - ActivityView
extension SleepActivityStatusCell {
    open class ActivityView: View {
        
        public let icon = ImageView()
        public let titleLabel = Label()
        
        open override func makeUI() {
            super.makeUI()
            
            titleLabel.font = Fonts.systemFont(ofSize: 12)
            titleLabel.textColor = UIColor.assist_A9A9A9
            
            icon.layer.cornerRadius = 4.0
            
            addSubview(icon)
            addSubview(titleLabel)
            
            
            icon.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.centerY.equalToSuperview()
                make.size.equalTo(8.0)
            }
            
            titleLabel.snp.makeConstraints { make in
                make.left.equalTo(icon.snp.right).offset(5)
                make.centerY.equalToSuperview()
                make.bottom.equalToSuperview()
                make.right.lessThanOrEqualToSuperview()
            }
            
        }
    
    }
    
}
